<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组合并示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }

        .container {
            width: 80%;
            margin: 50px auto;
            padding: 20px;
            background-color: #ffffff;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        .section {
            margin-top: 20px;
        }

        .section h2 {
            color: #555;
        }

        .list {
            padding-left: 0;
            list-style: none;
            margin: 10px 0;
        }

        .list li {
            background-color: #f9f9f9;
            margin: 8px 0;
            padding: 12px;
            border-radius: 6px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .combine-btn {
            display: block;
            width: 100%;
            padding: 12px;
            margin-top: 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            text-align: center;
        }

        .combine-btn:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            background-color: #eef;
            padding: 15px;
            border-radius: 6px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .result ul {
            padding-left: 0;
            list-style: none;
        }

        .result li {
            padding: 10px;
            background-color: #fff;
            margin: 6px 0;
            border-radius: 5px;
            border: 1px solid #ddd;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>数组合并示例</h1>

        <div class="section">
            <h2>数组 A</h2>
            <ul class="list" id="listA">
                <li>{"id": 1, "name": "苹果", "price": 5}</li>
                <li>{"id": 2, "name": "香蕉", "price": 3}</li>
            </ul>
        </div>

        <div class="section">
            <h2>数组 B</h2>
            <ul class="list" id="listB">
                <li>{"id": 2, "name": "香蕉", "price": 2}</li>
                <li>{"id": 3, "name": "橙子", "price": 4}</li>
            </ul>
        </div>

        <button class="combine-btn" onclick="combineArrays()">合并数组</button>

        <div class="result" id="resultSection" style="display:none;">
            <h2>合并后的结果</h2>
            <ul id="resultList"></ul>
        </div>
    </div>

    <script>
        // 合并函数
        const combine = (a, b, prop) =>
            Object.values(
                [...a, ...b].reduce((acc, v) => {
                    const item = v[prop];
                    if (item) {
                        acc[item] = acc[item] ? { ...acc[item], ...v } : { ...v };
                    }
                    return acc;
                }, {})
            );

        // 示例数据
        const arrayA = [
            { id: 1, name: "苹果", price: 5 },
            { id: 2, name: "香蕉", price: 3 }
        ];

        const arrayB = [
            { id: 2, name: "香蕉", price: 2 },
            { id: 3, name: "橙子", price: 4 }
        ];

        // 展示合并结果
        function combineArrays() {
            const result = combine(arrayA, arrayB, 'id');
            const resultSection = document.getElementById('resultSection');
            const resultList = document.getElementById('resultList');

            // 显示合并后的结果
            resultSection.style.display = 'block';
            resultList.innerHTML = '';

            result.forEach(item => {
                const li = document.createElement('li');
                li.textContent = JSON.stringify(item, null, 2);
                resultList.appendChild(li);
            });
        }
    </script>

</body>

</html>